home *** CD-ROM | disk | FTP | other *** search
Text File | 1996-04-04 | 1.4 KB | 40 lines | [TEXT/R*ch] |
- Keeping The Ports Straight
- Saving and restoring the GrafPort is something that must be done a lot. Suppose
- you needed to convert a Point to Local coordinates. You first have to set the
- GrafPort to the window who’s coordinates you want. It might look like this:
-
- void foo(WindowPtr myWindow, Point *thePoint) {
- GrafPrt savedPort; // declare a temporary variable
- GetPort(&savedPort); // remember the old port
- SetPort(myWindow); // point to the right window
- GlobalToLocal(thePoint); // do the work
- SetPort(savedPort); // restore the old port
- }
- Here’s a nifty C++ class to simplify things.
- Put it all in one header file, PortSaver.h:
-
- class PortSaver {
- public:
- PortSaver(GrafPtr newPort = nil);
- virtual ~PortSaver(void);
- private:
- GrafPtr savedPort;
- };
- inline PortSaver::PortSaver(GrafPtr newPort) {
- GetPort(&savedPort); // remember the old port
- if (newPort != nil)
- SetPort(newPort); // set the new port
- }
- inline PortSaver::~PortSaver(void) {
- if (savedPort != nil) // if there is an old port
- SetPort(savedPort); // restore it
- }
- // A macro that makes the PortSaver easier to use
- #define SETPORT(aPort) PortSaver setTheCurrentPortTo(aPort)
- Here is the same routine using class PortSaver
-
- void foo(WindowPtr myWindow, Point *thePoint) {
- SETPORT(myWindow); // point to the right window
- GlobalToLocal(thePoint); // do the work
- }
-